home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / STREAM13.ARJ / ENCRYPT.PAS < prev    next >
Pascal/Delphi Source File  |  1992-05-18  |  2KB  |  75 lines

  1. {$B-}   { Use fast boolean evaluation. }
  2.  
  3. program Encrypt;
  4.  
  5. { Program to demonstrate use of TEncryptFilter }
  6.  
  7. uses
  8.   {$ifdef windows}
  9.   wobjects,wincrt,
  10.   {$else}
  11.   objects,
  12.   {$endif}
  13.   streams;
  14.  
  15. procedure SyntaxExit(s:string);
  16. begin
  17.   writeln;
  18.   writeln(s);
  19.   writeln;
  20.   writeln('Usage:  ENCRYPT Sourcefile Destfile');
  21.   writeln(' will encrypt sourcefile using key $12345678.');
  22.   writeln(' Run ENCRYPT on the encrypted file to decrypt it.');
  23.   halt(99);
  24. end;
  25.  
  26. var
  27.   Source : PBufStream;
  28.   Dest   : PEncryptFilter;
  29.   filename : string;
  30. begin
  31.   if paramcount <> 2 then
  32.     SyntaxExit('Two parameters required.');
  33.  
  34.   { Open the source file with a buffer size of 2048. }
  35.  
  36.   {$ifdef windows}
  37.   Filename := Paramstr(1);
  38.   Filename[length(filename)+1] := #0;
  39.   New(Source, Init( @filename[1], stOpenRead, 2048) );
  40.   {$else}
  41.   New(Source, Init( Paramstr(1), stOpenRead, 2048) );
  42.   {$endif windows}
  43.  
  44.   if (Source = nil) or (Source^.status <> stOk) then
  45.     SyntaxExit('Unable to open file '+ParamStr(1)+' for reading.');
  46.  
  47.   { Open the destination file with a buffer size of 2048, and insert it
  48.     into the encrypting filter. }
  49.  
  50.   {$ifdef windows}
  51.   Filename := Paramstr(2);
  52.   Filename[length(filename)+1] := #0;
  53.   New(Dest,   Init($12345678, New(PBufStream,
  54.                                   Init( @filename[1], stCreate, 2048))));
  55.   {$else}                                             
  56.   New(Dest,   Init($12345678, New(PBufStream,
  57.                                   Init( Paramstr(2), stCreate, 2048))));
  58.   {$endif windows}
  59.   if (Dest = nil) or (Dest^.status <> stOk) then
  60.     SyntaxExit('Unable to create file '+Paramstr(2)+'.');
  61.  
  62.   { Encrypt the source file by copying it to the filter.}
  63.  
  64.   Write('Encrypting ',Paramstr(1),' to ',Paramstr(2),'...');
  65.   Dest^.CopyFrom(Source^, Source^.GetSize);
  66.   if Dest^.status <> stOK then
  67.     SyntaxExit('File error during encryption.');
  68.  
  69.   { Dispose of stream variables to close the files.}
  70.  
  71.   Dispose(Source, done);
  72.   Dispose(Dest, done);
  73.  
  74.   Writeln('Done.');
  75. end.